library _;

import 'package:flutter/material.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:pc/Const/IFinal.dart';
import 'package:pc/Model/UserModel.dart';
import 'package:pc/Util/App/AppUtils.dart';
import 'package:pc/Util/EncodeUtils.dart';

part 'controller.dart';

part 'view.dart';

class LoginPage extends YPage {
  String? codeParams;
  bool isShowPassword = false;
  final textController_account_loginname = TextEditingController(), textController_account_password = TextEditingController(), textController_account_code = TextEditingController(), focusNode_loginname = FocusNode(); //账号登录

  @override
  void yOnCreate() {
    focusNode_loginname.addListener(() {
      if (!focusNode_loginname.hasFocus) controller_refreshCode();
    });
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.fromLTRB(100, 50, 100, 50),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(YConfig.appName, style: TextStyle(fontSize: 50, fontWeight: FontWeight.bold, color: YConfig.themeColor)),
          YSpacer(height: 50),
          TextFormField(
            controller: textController_account_loginname,
            decoration: InputDecoration(
              hintText: "请输入用户名",
              hintStyle: TextStyle(color: Colors.grey),
              enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.grey.shade400)),
            ),
            focusNode: focusNode_loginname,
          ),
          YSpacer(height: 20),
          TextFormField(
            controller: textController_account_password,
            obscureText: !isShowPassword,
            decoration: InputDecoration(
              hintText: "请输入密码",
              hintStyle: TextStyle(color: Colors.grey),
              enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.grey.shade400)),
              suffixIcon: YButton(
                width: 50,
                child: Image.asset(isShowPassword ? "images/ic_eye_open.png" : "images/ic_eye_close.png", width: 20),
                onTap: () => setState(() => isShowPassword = !isShowPassword),
              ),
            ),
          ),
          YSpacer(height: 20),
          TextFormField(
            controller: textController_account_code,
            decoration: InputDecoration(
              hintText: "请输入验证码",
              hintStyle: TextStyle(color: Colors.grey),
              enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.grey.shade400)),
              suffixIcon: codeParams == null || codeParams!.isEmpty
                  ? YEmpty()
                  : GestureDetector(
                      child: Container(
                        padding: EdgeInsets.all(5),
                        child: YImage(null, 0, "${IFinal.Others.BaseIP}${IFinal.Urls.verifyCode}$codeParams"),
                      ),
                      onTap: () => controller_refreshCode(),
                    ),
            ),
          ),
          YSpacer(height: 30),
          view_login(),
        ],
      ),
    );
  }
}
